package prassein.rsm.service.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.common.utils.KeyWordsReader;
import prassein.rsm.dao.base.ResumeDao;
import prassein.rsm.dto.FilterDto;
import prassein.rsm.entity.base.Resume;

import com.opensymphony.xwork2.ActionContext;

@Service
public class ResumeManager {

	@Autowired
	private ResumeDao resumeDao;
	
	/**
	 * 取得所有角色信息
	 */
	public List<Resume> getResumes(){
		List<Criterion> criterions = new ArrayList<Criterion>();
		
		criterions.add(Restrictions.not(Restrictions.eq("state", Constants.DELETED)));
		
		return resumeDao.query(criterions.toArray(new Criterion[criterions.size()]));
	}
	
	public List<Resume> getAllResumes(){
		return resumeDao.getAll();
	}
	
	public Resume getResume(Serializable id){
		return resumeDao.get(id);
	}
	
	
	public List<Resume> getAllDesc() {
		return resumeDao.getAllDesc();
	}
	
	public void save(Resume resume) {
		resumeDao.save(resume);
	}

	public ResumeDao getResumeDao() {
		return resumeDao;
	}

	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}

	@SuppressWarnings("unchecked")
	public List<Resume> queryByPage(int pageSize, int pageNow) {
		DetachedCriteria beautyCriteria = DetachedCriteria.forClass(Resume.class).createCriteria("id");
		beautyCriteria.add(Restrictions.isNotNull("id"));
		return resumeDao.findPageByCriteria(beautyCriteria, pageSize, pageNow).getItems();
	}
	
	public int queryTotalCount() {
		resumeDao.findPageByCriteria(null).getTotalCount();
		return 0;
	}
	
	public List<Resume> queryResult(FilterDto filterResume){
		List<Criterion> criterions = new ArrayList<Criterion>();
		
		criterions.addAll(GetIgnCriterion());
		
		//criterions.addAll(GetAgeCriterion(filterResume.getAge()));
		
//		if(StringUtils.isNotBlank(requirement.getTypeOfWork())) {
//			criterions.add(Restrictions.like("typeOfWork", "%" + requirement.getTypeOfWork() + "%"));
//		}
		if(StringUtils.isNotBlank(filterResume.getName())) {
			criterions.add(Restrictions.like("name", "%" + filterResume.getName() + "%"));
		}
//		if(StringUtils.isNotBlank(filterResume.getNationality())) {
//			criterions.add(Restrictions.like("nationality", "%" + filterResume.getNationality() + "%"));
//		}
		if(StringUtils.isNotBlank(filterResume.getCorporation())) {
			criterions.add(Restrictions.like("corporation", "%" + filterResume.getCorporation() + "%"));
		}
		if(StringUtils.isNotBlank(filterResume.getPosition())) {
			criterions.add(Restrictions.like("position", "%" + filterResume.getPosition() + "%"));
		}
		if(StringUtils.isNotBlank(filterResume.getIndustry())) {
			criterions.add(Restrictions.like("industry", "%" + filterResume.getIndustry() + "%"));
		}
		if(StringUtils.isNotBlank(filterResume.getSex())) {
			criterions.add(Restrictions.like("sex", "%" + filterResume.getSex() + "%"));
		}
		if(StringUtils.isNotBlank(filterResume.getWorkingPlace())) {
			criterions.add(Restrictions.like("workingPlace", "%" + filterResume.getWorkingPlace() + "%"));
		}
//		if(StringUtils.isNotBlank(filterResume.getWorkSeniority())) {
//			criterions.add(Restrictions.like("workSeniority", "%" + filterResume.getWorkSeniority() + "%"));
//		}
		
		return resumeDao.query(criterions.toArray(new Criterion[criterions.size()]));
	}
	
//	private List<Criterion> GetAgeCriterion(String agefilter) {
//		List<Criterion> criterions = new ArrayList<Criterion>();
//		
//		agefilter = agefilter.trim();
//		int smaller = agefilter.indexOf("<");
//		int bigger = agefilter.indexOf(">");
//		int equal = agefilter.indexOf("=");
//		int between = agefilter.indexOf("-");
//		
//		if (smaller == 0) {
//			criterions.add(Restrictions.not(Restrictions.lt("dateOfBirth", agefilter.replace("<", "").trim())));
//		}
//		if (bigger == 0) {
//			criterions.add(Restrictions.not(Restrictions.gt("dateOfBirth", agefilter.replace(">", "").trim())));
//		}
//		if (equal == 0) {
//			criterions.add(Restrictions.not(Restrictions.eq("dateOfBirth", agefilter.replace("=", "").trim())));
//		}
//		if (between > 0) {
//			String age1 = agefilter.substring(0, between).trim();
//			String age2 = agefilter.substring(between + 1, agefilter.length()).trim();
//			
//			criterions.add(Restrictions.not(Restrictions.gt("dateOfBirth", age1)));
//			criterions.add(Restrictions.not(Restrictions.lt("dateOfBirth", age2)));
//
//		}
//		
//		return criterions;
//	}
	
	private List<Criterion> GetIgnCriterion() {
		List<Criterion> criterions = new ArrayList<Criterion>();
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		int permLevel = Integer.parseInt(session.get(Constants.PERM_LEVEL).toString());
		if ((permLevel % 10000) <= 1000) {
			String keywordString = KeyWordsReader.getInstance().getConfigItem("ignoreWords");
			String[] keywords = keywordString.split(";");
			
			if (keywords != null) {
				for (String keyword : keywords) {
					if (keyword != null && !keyword.trim().equals("")) {
						criterions.add(Restrictions.not(Restrictions.like("position", "%" + keyword.trim() + "%")));
					}
				}
			}
		}
		return criterions;
	}
	
	public List<Resume> queryResult(String name, String contactNumber){
		List<Criterion> criterions = new ArrayList<Criterion>();
		if(StringUtils.isNotBlank(name)) {
			criterions.add(Restrictions.like("name", name));
		}
		if(StringUtils.isNotBlank(name)) {
			criterions.add(Restrictions.eq("contactNumber", contactNumber));
		}
		return resumeDao.query(criterions.toArray(new Criterion[criterions.size()]));
	}
}
